package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserServiceTest {



    @Test
    public void findAll(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        /*分页插件的使用*/
        PageHelper.startPage(1,3);
        List<User> all = mapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        PageInfo<User> pif = new PageInfo<>(all);
        System.out.println(pif);
        sqlSession.close();
    }

    @Test
    public void findUser(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.findUser("辽神", 1);
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void findUsersById(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Integer> list = new ArrayList<>();
        List<User> users = mapper.findUsersById(list);
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void findOneByCondition(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User userl =new User();
        userl.setUsername("张三");
        userl.setPassword("666");
        List<User> users = mapper.findOneByCondition(userl);
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }


    @Test
    public void updateUserCondition(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User userl =new User();
        userl.setId(1);
        userl.setPassword("777");
        mapper.updateUserCondition(userl);
        sqlSession.close();
    }






}
